Skip to content

Conversation

@Ericson2314
Copy link
Member

@Ericson2314 Ericson2314 commented Nov 13, 2025

Motivation

Trying out new and improved FFI with new and improved bindings + Harmonia.

Context

Rebase of #14031
Depends on #14560


Add 👍 to pull requests you find important.

The Nix maintainer team uses a GitHub project board to schedule and track reviews.

@github-actions github-actions bot added the c api Nix as a C library with a stable interface label Nov 13, 2025
@github-actions github-actions bot added new-cli Relating to the "nix" command with-tests Issues related to testing. PRs with tests have some priority labels Nov 13, 2025
@Ericson2314 Ericson2314 marked this pull request as ready for review November 13, 2025 21:38
Copy link
Member

@roberth roberth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C API review only

nix_c_context * context,
Store * store,
const StorePath * path,
void (*callback)(void * userdata, const nix_derivation * drv),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's propagate the error. I'd say it's more useful for early abort scenarios (multiple calls), but still useful here to use the same error handling mechanism.

Suggested change
void (*callback)(void * userdata, const nix_derivation * drv),
nix_err (*callback)(nix_c_context * return_ctx, void * userdata, const nix_derivation * drv),

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since derivations internally heap allocate, I think I will just make this return a pointer to a derivation on the heap too.

@Ericson2314
Copy link
Member Author

Ericson2314 commented Nov 17, 2025

OK I moved some of the new stuff in to https://github.com/NixOS/nix/tree/more-store-ffi-2 so this is smaller in scope.

Ericson2314 and others added 2 commits November 20, 2025 00:49
These do a read/write test in the middles of some computation. They are
an imperative way to test intermediate values rather than functionally
testing end outputs.
`nix derivation add`, and its C API counterpart, now works a bit closer
to `builtins.derivation` in that they don't require the user to fill-in
input addressed paths correctly ahead of time.

The logic for this is carefully deduplicated, between all 3 entry
points, and also between the existing `checkInvariants` function. There
are some more functional tests, and there are also many more unit tests.

Co-authored-by: Sergei Zimmerman <[email protected]>
Co-authored-by: edef <[email protected]>
@Ericson2314 Ericson2314 changed the title libstore-c: Organize, and flesh out API libstore-c: Add new derivation and store path functions Nov 20, 2025
Add several new functions to the C API:

StorePath operations:
- nix_store_path_hash: Extract the hash part from a store path
- nix_store_create_from_parts: Construct a store path from hash and name

Derivation operations:
- nix_derivation_clone: Clone a derivation
- nix_derivation_to_json: Serialize a derivation to JSON

Store operations:
- nix_store_drv_from_store_path: Load a derivation from a store path

Test the new functions, and improve documentation of some existing
functions to better distinguish them, also.

Co-authored-by: Tristan Ross <[email protected]>
Co-authored-by: Robert Hensing <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c api Nix as a C library with a stable interface new-cli Relating to the "nix" command with-tests Issues related to testing. PRs with tests have some priority

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants